const { sequelize } = require('../utils/database');
const { User, Activity, UserProfile, ActivityParticipant } = require('../models');

async function seed() {
  try {
    console.log('开始创建种子数据...');
    
    // 测试数据库连接
    await sequelize.authenticate();
    console.log('数据库连接成功');
    
    // 检查是否已有数据
    const existingUsers = await User.count();
    if (existingUsers > 0) {
      console.log('数据库中已有用户数据，重新创建种子数据...');
      
      // 删除现有的种子数据
      await User.destroy({ where: {} });
      await UserProfile.destroy({ where: {} });
      await Activity.destroy({ where: {} });
      await ActivityParticipant.destroy({ where: {} });
      console.log('已清理现有数据');
    }
    
    // 逐个创建用户以确保密码正确加密
    console.log('创建管理员用户...');
    const admin = await User.create({
      username: 'admin',
      email: 'admin@campus.com',
      password_hash: 'admin123',
      bio: '系统管理员',
      wallet_address: '0x1234567890123456789012345678901234567890'
    });
    
    console.log('创建测试用户Alice...');
    const alice = await User.create({
      username: 'alice',
      email: 'alice@student.com',
      password_hash: 'password123',
      bio: '热爱运动的大学生',
      wallet_address: '0x2345678901234567890123456789012345678901'
    });
    
    console.log('创建测试用户Bob...');
    const bob = await User.create({
      username: 'bob',
      email: 'bob@student.com',
      password_hash: 'password123',
      bio: '喜欢学术活动',
      wallet_address: '0x3456789012345678901234567890123456789012'
    });
    
    console.log('创建测试用户Charlie...');
    const charlie = await User.create({
      username: 'charlie',
      email: 'charlie@student.com',
      password_hash: 'password123',
      bio: '社交达人',
      wallet_address: '0x4567890123456789012345678901234567890123'
    });
    
    const users = [admin, alice, bob, charlie];
    console.log('用户创建完成');
    
    // 创建用户画像
    await UserProfile.bulkCreate([
      {
        user_id: users[0].id,
        interests: ['管理', '技术', '教育'],
        activity_preferences: { type: 'all' },
        activity_frequency: 'high',
        social_level: 'extrovert'
      },
      {
        user_id: users[1].id,
        interests: ['运动', '健身', '户外'],
        activity_preferences: { type: 'sports' },
        location_preference: '体育馆',
        activity_frequency: 'high',
        social_level: 'extrovert'
      },
      {
        user_id: users[2].id,
        interests: ['学术', '科技', '编程'],
        activity_preferences: { type: 'academic' },
        location_preference: '图书馆',
        activity_frequency: 'medium',
        social_level: 'introvert'
      },
      {
        user_id: users[3].id,
        interests: ['社交', '音乐', '艺术'],
        activity_preferences: { type: 'social' },
        activity_frequency: 'high',
        social_level: 'extrovert'
      }
    ]);
    
    console.log('用户画像创建完成');
    
    // 创建测试活动
    const activities = await Activity.bulkCreate([
      {
        title: '校园马拉松比赛',
        description: '一年一度的校园马拉松比赛，欢迎所有同学参与！',
        location: '校园操场',
        start_time: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7天后
        end_time: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000), // 7天后+4小时
        max_participants: 100,
        created_by: users[0].id,
        status: 'published',
        tags: ['运动', '健身', '比赛'],
        latitude: 39.9042,
        longitude: 116.4074
      },
      {
        title: '人工智能技术讲座',
        description: '邀请业界专家分享最新的AI技术发展趋势',
        location: '学术报告厅',
        start_time: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000), // 3天后
        end_time: new Date(Date.now() + 3 * 24 * 60 * 60 * 1000 + 2 * 60 * 60 * 1000), // 3天后+2小时
        max_participants: 200,
        created_by: users[1].id,
        status: 'published',
        tags: ['学术', '科技', 'AI'],
        latitude: 39.9042,
        longitude: 116.4074
      },
      {
        title: '校园音乐节',
        description: '学生社团联合举办的音乐节，多个乐队现场演出',
        location: '大学生活动中心',
        start_time: new Date(Date.now() + 14 * 24 * 60 * 60 * 1000), // 14天后
        end_time: new Date(Date.now() + 14 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000), // 14天后+6小时
        max_participants: 500,
        created_by: users[2].id,
        status: 'published',
        tags: ['音乐', '艺术', '社交'],
        latitude: 39.9042,
        longitude: 116.4074
      },
      {
        title: '编程竞赛',
        description: 'ACM程序设计竞赛校内选拔赛',
        location: '计算机实验室',
        start_time: new Date(Date.now() + 10 * 24 * 60 * 60 * 1000), // 10天后
        end_time: new Date(Date.now() + 10 * 24 * 60 * 60 * 1000 + 5 * 60 * 60 * 1000), // 10天后+5小时
        max_participants: 50,
        created_by: users[3].id,
        status: 'published',
        tags: ['编程', '竞赛', '学术'],
        latitude: 39.9042,
        longitude: 116.4074
      }
    ]);
    
    console.log('活动创建完成');
    
    // 创建一些参与记录
    await ActivityParticipant.bulkCreate([
      {
        user_id: users[1].id,
        activity_id: activities[0].id,
        status: 'registered'
      },
      {
        user_id: users[2].id,
        activity_id: activities[1].id,
        status: 'registered'
      },
      {
        user_id: users[3].id,
        activity_id: activities[2].id,
        status: 'registered'
      },
      {
        user_id: users[1].id,
        activity_id: activities[3].id,
        status: 'registered'
      }
    ]);
    
    // 更新活动参与人数
    for (const activity of activities) {
      const participantCount = await ActivityParticipant.count({
        where: { activity_id: activity.id }
      });
      await activity.update({ current_participants: participantCount });
    }
    
    console.log('参与记录创建完成');
    console.log('种子数据创建完成！');
    
    console.log('\n=== 测试账户信息 ===');
    console.log('管理员: admin@campus.com / admin123');
    console.log('用户1: alice@student.com / password123');
    console.log('用户2: bob@student.com / password123');
    console.log('用户3: charlie@student.com / password123');
    
    process.exit(0);
  } catch (error) {
    console.error('创建种子数据失败:', error);
    process.exit(1);
  }
}

seed(); 